#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int count = 0;
int n;
bool vis[11];
int request[11];
void dfs(int num)
{
    if (num == n + 1)
    {
        count++;
        return;
    }
    for (int i = 1;i <= n;i++)
    {
        if (!vis[i] && (!vis[request[i]] || request[i] == i))
        {
            vis[i] = 1;
            dfs(num + 1);
            vis[i] = 0;
        }
    }

}
int main()
{
    memset(vis, 0, sizeof vis);
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        cin >> request[i];
    }
    dfs(1);
    cout << count;




    return 0;
}
